package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

import static cn.tedu._02notice.base.response.StatusCode.OPERATION_FAILED;
import static cn.tedu._02notice.base.response.StatusCode.OPERATION_SUCCESS;

//@Controller
@RestController
@RequestMapping("/v1/notice/")
@Api(tags = "01.资讯管理模块")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
//    @ResponseBody
    public JsonResult add(NoticeAddParam noticeAddParam) {
        System.out.println("noticeAddParam = " + noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(10086L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);

        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("资讯列表")
    @ApiOperationSupport(order = 20)
//    @ResponseBody
    public JsonResult list(NoticeAddQuery noticeAddQuery) {
        System.out.println(noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOList = noticeMapper.list(title, type);

        return JsonResult.ok(noticeListVOList);

    }

    @GetMapping("detail")
    @ApiOperation("咨询详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "咨询编号", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "咨询内容")
    })
//    @ResponseBody
    public NoticeDetailInfoVO detail(Long id, String title) {
        System.out.println(id);
        return noticeMapper.detail(id);
    }

    @PostMapping("del")
    @ApiOperation("删除咨询")
    @ApiOperationSupport(order = 40)
//    @ResponseBody
    public String del(Long id) {
        System.out.println(id);
        return noticeMapper.del(id) > 0 ? "删除公告成功！" : "删除失败！";
    }

    @PostMapping("update")
    @ApiOperation("修改咨询")
    @ApiOperationSupport(order = 50)
//    @ResponseBody
    public String update(NoticeUpdateParam noticeUpdateParam) {
        System.out.println(noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());

        return noticeMapper.update(notice) > 0 ? "修改公告成功！" : "修改失败！";
    }

}
